#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Redis COM Component Configuration
Redis COM组件配置文件
"""

# Redis服务器配置
REDIS_CONFIG = {
    'host': '10.0.0.204',      # Redis服务器地址
    'port': 6379,              # Redis端口
    'password': '123456',      # Redis密码
    'decode_responses': True,  # 自动解码响应
    'socket_timeout': 30,      # 连接超时时间（秒）
    'socket_connect_timeout': 30,  # 连接建立超时时间（秒）
}

# COM组件配置
COM_CONFIG = {
    'version': '1.0.0',
    'timeout': 30,             # 操作超时时间（秒）
    'max_keys_return': 50,     # 键列表最大返回数量
    'default_expire': 3600,    # 默认过期时间（秒）
}

# 日志配置
LOG_CONFIG = {
    'enable_logging': True,     # 是否启用日志
    'log_level': 'INFO',       # 日志级别: DEBUG, INFO, WARNING, ERROR
    'log_file': 'redis_com.log',  # 日志文件名
}

# 安全配置
SECURITY_CONFIG = {
    'enable_base64_encoding': True,  # 启用Base64编码（推荐）
    'max_value_size': 1024 * 1024,  # 最大值大小（1MB）
    'allowed_key_patterns': ['*'],   # 允许的键模式
}

# 性能配置
PERFORMANCE_CONFIG = {
    'connection_pool_size': 10,      # 连接池大小
    'retry_attempts': 3,             # 重试次数
    'retry_delay': 1,                # 重试延迟（秒）
}

def get_redis_config():
    """获取Redis配置"""
    return REDIS_CONFIG.copy()

def get_com_config():
    """获取COM配置"""
    return COM_CONFIG.copy()

def get_log_config():
    """获取日志配置"""
    return LOG_CONFIG.copy()

def get_security_config():
    """获取安全配置"""
    return SECURITY_CONFIG.copy()

def get_performance_config():
    """获取性能配置"""
    return PERFORMANCE_CONFIG.copy()

# 配置验证函数
def validate_config():
    """验证配置有效性"""
    errors = []

    # 验证Redis配置
    if not REDIS_CONFIG['host']:
        errors.append("Redis host cannot be empty")

    if not isinstance(REDIS_CONFIG['port'], int) or REDIS_CONFIG['port'] <= 0:
        errors.append("Redis port must be a positive integer")

    if COM_CONFIG['timeout'] <= 0:
        errors.append("COM timeout must be positive")

    return errors

# 配置更新函数
def update_redis_config(host=None, port=None, password=None):
    """更新Redis配置"""
    global REDIS_CONFIG

    if host is not None:
        REDIS_CONFIG['host'] = host
    if port is not None:
        REDIS_CONFIG['port'] = port
    if password is not None:
        REDIS_CONFIG['password'] = password

def update_com_config(timeout=None, max_keys=None):
    """更新COM配置"""
    global COM_CONFIG

    if timeout is not None:
        COM_CONFIG['timeout'] = timeout
    if max_keys is not None:
        COM_CONFIG['max_keys_return'] = max_keys

if __name__ == '__main__':
    # 配置验证
    errors = validate_config()
    if errors:
        print("❌ Configuration errors found:")
        for error in errors:
            print(f"  - {error}")
    else:
        print("✅ Configuration is valid")

    # 显示当前配置
    print("\n📋 Current Configuration:")
    print(f"Redis Server: {REDIS_CONFIG['host']}:{REDIS_CONFIG['port']}")
    print(f"COM Version: {COM_CONFIG['version']}")
    print(f"Timeout: {COM_CONFIG['timeout']}s")
    print(f"Logging: {'Enabled' if LOG_CONFIG['enable_logging'] else 'Disabled'}")
    print(f"Security: {'Base64 Encoding' if SECURITY_CONFIG['enable_base64_encoding'] else 'Plain Text'}")
